<template>
    <el-dialog v-model="dialogFormVisible" :title="title" @close="closeDialog">
        <el-form :model="addApprovalReportForm" ref="addApprovalReportRef" :rules="addApprovalReportrules">
                        <el-form-item label="填报日期" :label-width="formLabelWidth"
                                      prop="fillDate" v-if="addApprovalReportForm.id">
                            <el-input
                                    v-model="addApprovalReportForm.fillDate"
                                    autocomplete="off"
                                    ref="name"
                                    placeholder="请填写填报日期"
                            ></el-input>
                        </el-form-item>
                        <el-form-item label="提交时间" :label-width="formLabelWidth"
                                      prop="submitTime" v-if="addApprovalReportForm.id">
                            <el-input
                                    v-model="addApprovalReportForm.submitTime"
                                    autocomplete="off"
                                    ref="name"
                                    placeholder="请填写提交时间"
                            ></el-input>
                        </el-form-item>
                        <el-form-item label="上报人工号" :label-width="formLabelWidth"
                                      prop="teacherNumber" v-if="addApprovalReportForm.id">
                            <el-input
                                    v-model="addApprovalReportForm.teacherNumber"
                                    autocomplete="off"
                                    ref="name"
                                    placeholder="请填写上报人工号"
                            ></el-input>
                        </el-form-item>
                        <el-form-item label="上报人姓名" :label-width="formLabelWidth"
                                      prop="teacherName">
                            <el-input
                                    v-model="addApprovalReportForm.teacherName"
                                    autocomplete="off"
                                    ref="name"
                                    placeholder="请填写上报人姓名"
                            ></el-input>
                        </el-form-item>
                        <el-form-item label="上报人部门" :label-width="formLabelWidth"
                                      prop="department">
                            <el-input
                                    v-model="addApprovalReportForm.department"
                                    autocomplete="off"
                                    ref="name"
                                    placeholder="请填写上报人部门"
                            ></el-input>
                        </el-form-item>
                        <el-form-item label="上报问题" :label-width="formLabelWidth"
                                      prop="reportIssue" v-if="addApprovalReportForm.id">
                            <el-input
                                    v-model="addApprovalReportForm.reportIssue"
                                    autocomplete="off"
                                    ref="name"
                                    placeholder="请填写上报问题"
                            ></el-input>
                        </el-form-item>
                        <el-form-item label="处理结果" :label-width="formLabelWidth"
                                      prop="processResult" v-if="addApprovalReportForm.id">
                            <el-input
                                    v-model="addApprovalReportForm.processResult"
                                    autocomplete="off"
                                    ref="name"
                                    placeholder="请填写处理结果"
                            ></el-input>
                        </el-form-item>
                        <el-form-item label="校区" :label-width="formLabelWidth"
                                      prop="campus">
                            <el-input
                                    v-model="addApprovalReportForm.campus"
                                    autocomplete="off"
                                    ref="name"
                                    placeholder="请填写校区"
                            ></el-input>
                        </el-form-item>
                        <el-form-item label="上报图片" :label-width="formLabelWidth"
                                      prop="reportImage">
                            <el-input
                                    v-model="addApprovalReportForm.reportImage"
                                    autocomplete="off"
                                    ref="name"
                                    placeholder="请填写上报图片"
                            ></el-input>
                        </el-form-item>
        </el-form>
        <template #footer>
      <span class="dialog-footer">
        <el-button @click="cancel()">取消</el-button>
        <el-button type="primary" @click="save()">保存</el-button>
      </span>
        </template>
    </el-dialog>
</template>

<script>
    import {ref} from "vue";
    import {httpService} from "@/utils/httpService";
    import {ElMessage, ElForm} from "element-plus";
    import {vxRule} from "@/utils/validator";

    export default {
        name: "AddApprovalReport",
        components: {ElForm, ElMessage},
        data() {
            return {
                dialogFormVisible: ref(false),
                formLabelWidth: "120px",
                title: "",
                personnelType: "",
                addApprovalReportForm: {
                    fillDate: "",
                    submitTime: "",
                    teacherNumber: "",
                    teacherName: "",
                    department: "",
                    reportIssue: "",
                    processResult: "",
                    campus: "",
                    reportImage: "",
                },
                // 校验规则
                addApprovalReportrules: {
                    teacherName: vxRule(true, null, "blur"),
                    department: vxRule(true, null, "blur"),
                    campus: vxRule(true, null, "blur"),
                    reportImage: vxRule(true, null, "blur"),
                },
            };
        },
        methods: {
            closeDialog() {
                this.clear();
            },
            cancel() {
                this.dialogFormVisible = false;
                this.clear();
            },
            getApprovalReportById(id) {
                httpService("/api/approvalReport/queryById", {id: id}, "get").then(
                    (data) => {
                        this.addApprovalReportForm = data;
                    }
                );
            },
            clear() {
                this.dialogFormVisible = false;
                //清空form
                    this.addApprovalReportForm.id = "";
                    this.addApprovalReportForm.fillDate = "";
                    this.addApprovalReportForm.submitTime = "";
                    this.addApprovalReportForm.teacherNumber = "";
                    this.addApprovalReportForm.teacherName = "";
                    this.addApprovalReportForm.department = "";
                    this.addApprovalReportForm.reportIssue = "";
                    this.addApprovalReportForm.processResult = "";
                    this.addApprovalReportForm.campus = "";
                    this.addApprovalReportForm.reportImage = "";
            },
            save() {
                this.$refs.addApprovalReportRef.validate((valid) => {
                    if (valid) {
                        let params = new FormData(); //创建一个form对象,必须是form对象否则后端接受不到数据
                        params.append("fillDate", this.addApprovalReportForm.fillDate);
                        params.append("submitTime", this.addApprovalReportForm.submitTime);
                        params.append("teacherNumber", this.addApprovalReportForm.teacherNumber);
                        params.append("teacherName", this.addApprovalReportForm.teacherName);
                        params.append("department", this.addApprovalReportForm.department);
                        params.append("reportIssue", this.addApprovalReportForm.reportIssue);
                        params.append("processResult", this.addApprovalReportForm.processResult);
                        params.append("campus", this.addApprovalReportForm.campus);
                        params.append("reportImage", this.addApprovalReportForm.reportImage);
                        if (this.addApprovalReportForm.id) {
                            params.append("id", this.addApprovalReportForm.id);
                            httpService("/api/approvalReport/edit", params, "post").then(
                                (data) => {
                                    if (data.code == 200) {
                                        ElMessage.success("修改成功");
                                        this.clear();
                                        //刷新父组件的数据
                                        this.$parent.getApprovalReportList();
                                    } else {
                                        ElMessage.error("修改失败");
                                    }
                                }
                            );
                        } else {
                            httpService("/api/approvalReport/add", params, "post").then(
                                (data) => {
                                    if (data.code == 200) {
                                        ElMessage.success("添加成功");
                                        this.clear();
                                        //刷新父组件的数据
                                        this.$parent.getApprovalReportList();
                                    } else {
                                        ElMessage.error("添加失败");
                                    }
                                }
                            );
                        }
                    } else {
                        return false;
                    }
                });
            },
        },
    };
</script>
<style>
</style>